package com.shopx.cm.service.impl;


import com.shopx.base.constants.ResultCodeEnum;
import com.shopx.base.exception.ShopxException;
import com.shopx.base.service.ISysSeqService;
import com.shopx.cm.bean.CmSync;
import com.shopx.cm.dao.db1.CmSyncDao;
import com.shopx.cm.model.CmSyncModel;
import com.shopx.cm.service.ICmSyncService;
import com.shopx.cmm.DTO.PageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 *
 * @author lituo
 * @className: CmSync
 * @date 2025-01-07
 */
@Service
@Slf4j
public class CmSyncServiceImpl implements ICmSyncService {

    @Resource
    private CmSyncDao cmSyncDao;

    @Resource
    private ISysSeqService sysSeqService;

    @Override
    public CmSyncModel selectByPrimaryKey(CmSync cmSync) {
        if (cmSync == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        return cmSyncDao.selectByPrimaryKey(cmSync);
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int insertSelective(CmSync cmSync) {
        if (cmSync == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        String id = sysSeqService.getTabSeq("CmSync", 4, null);
        cmSync.setCmsId(id);
        int result = cmSyncDao.insertSelective(cmSync);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(CmSync cmSync) {
        if (cmSync == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = cmSyncDao.updateByPrimaryKeySelective(cmSync);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int deleteByPrimaryKey(CmSync cmSync) {
        if (cmSync == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = cmSyncDao.deleteByPrimaryKey(cmSync);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    public int selectCount(CmSyncModel cmSync) {
        return cmSyncDao.selectCount(cmSync);
    }

    @Override
    public List<CmSyncModel> selectList(CmSyncModel cmSync) {
        List<CmSyncModel> list = cmSyncDao.selectList(cmSync);
        return list;
    }

    @Override
    public PageDTO<CmSyncModel> getListPage(CmSyncModel cmSync) {
        if (cmSync == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        PageDTO<CmSyncModel> pageDTO = new PageDTO<CmSyncModel>();
        pageDTO.setiTotalDisplayRecords(selectCount(cmSync));
        pageDTO.setData(selectList(cmSync));
        return pageDTO;
    }

}

